package com.pe.pgn.clubpgn.webapp.controller.topes;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.beans.BNClienteEmpresa;
import com.pe.pgn.clubpgn.domain.beans.BNClienteEmpresaBusqueda;
import com.pe.pgn.clubpgn.service.ClienteEmpresaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/topes/**")
public class TopesController extends BaseFormController{

	@Autowired
	private ClienteEmpresaService clienteEmpresaService;
	
	@RequestMapping("/topeslist.html")
	public void listarEmpresas(@ModelAttribute("clientebusqueda") BNClienteEmpresaBusqueda clientebusqueda,
			@RequestParam(value = "search", required = false) String search, Model model) {
		
		if(search != null && StringUtils.isNotBlank(search)){
			List<BNClienteEmpresa> empresas = clienteEmpresaService.busquedaClientesPorFiltrosConLineaCredito(
					clientebusqueda.getDeNombreEmpresa(), clientebusqueda.getDeRuc(), clientebusqueda.getLineaCreditoMinima(), 
					clientebusqueda.getLineaCreditoMaxima(), clientebusqueda.getTipoLineaCredito(), clientebusqueda.getStHabilitado());
			model.addAttribute("empresas",empresas);
			model.addAttribute("clientebusqueda",clientebusqueda);
		}
		else{
			List<BNClienteEmpresa> empresas = clienteEmpresaService.obtenerClienteEmpresasConLineaCredito(Boolean.TRUE);
			model.addAttribute("clientebusqueda", new BNClienteEmpresaBusqueda());        
			model.addAttribute("empresas", empresas);
		}
	}
	
	@RequestMapping(value = "/topesform.html", method = RequestMethod.GET)
	public @ModelAttribute("clienteempresa") BNClienteEmpresa verClienteEmpresaTope(
				@RequestParam(value = "id", required = true) Long id, HttpServletRequest request,ModelMap modelMap) {
		
		BNClienteEmpresa clienteEmpresa= clienteEmpresaService.obtenerClienteEmpresaTope(id);
		return clienteEmpresa;
	}
	
	@RequestMapping(value = "/eliminaclienteempresatopeform.html", method = RequestMethod.GET)
	public String eliminarTope(@RequestParam(value = "id", required = true) Long id ,
				@RequestParam(value = "idCliente", required = true) Long idCliente
				) {
		
		clienteEmpresaService.eliminarTope(id);
		return "redirect:topesform.html?id=" + idCliente;
	}
	
	@RequestMapping(value = "/topesform.html", method = RequestMethod.POST)
	public String guardarTopeCliente(@ModelAttribute("clienteempresa") BNClienteEmpresa clienteempresa,
			BindingResult result, SessionStatus status, HttpServletRequest request) {

		try {
			if (result.hasErrors()) {
				return "/topes/topesform";
			}
			
			clienteEmpresaService.guardarClienteEmpresaTope(clienteempresa);
			saveMessage(request, getText("clienteempresatopeform.added",request.getLocale()));
			return "redirect:topesform.html?id=" + clienteempresa.getId();
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/topes/topesform";
		}
	}
}
